package ext.tianma.part.report.searchAPL.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.dom4j.DocumentException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import wt.util.WTException;
import wt.util.WTRuntimeException;
import ext.tianma.part.report.searchAPL.service.APLConfig;
import ext.tianma.part.report.searchAPL.service.APLService;

/**
 * 类名: APLController </br>
 * 包名: ext.tianma.part.report.searchAPL.controller
 * 描述: 报表-APL材料
 * @author xucr
 * 创建时间: 2020年6月16日
 * 发布版本: V1.0
 */
@Controller
@RequestMapping({ "/APLController" })
public class APLController {
	
	@Autowired
	APLService aplservice;
	
	/**
	 * 查询APL材料
	 * @param params
	 * @param response
	 * @throws Exception
	 * @author xucr
	 * @CreateDate 2020年6月23日
	 */
	@RequestMapping(value = "/queryAPL", method = {RequestMethod.POST, RequestMethod.GET })
	public void queryAPL(@RequestParam(value = "params", required = false) String params,
			HttpServletResponse response) throws Exception {
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
			String result = aplservice.queryAPL(params);
			out.print(result);
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	
	@RequestMapping(value = "/queryAPLEasuyUI", method = {RequestMethod.POST, RequestMethod.GET })
	public void queryAPLEasuyUI(@RequestParam(value = "params", required = false) String params,
			HttpServletResponse response) throws Exception {
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
			String result = aplservice.queryAPLEasyUI(params);
			out.print(result);
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	
	@RequestMapping(value = "/queryAPLLayUI", method = {RequestMethod.POST, RequestMethod.GET })
	public void queryAPLLayUI(@RequestParam(value = "params", required = false) String params,
			HttpServletResponse response) throws Exception {
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
			String result = aplservice.queryAPLLayUI(params);
			out.print(result);
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	
	/**
	 * 导出Excel
	 * @param params
	 * @param response
	 * @throws Exception
	 * @author xucr
	 * @CreateDate 2020年6月24日
	 */
	@RequestMapping(value = "/export", method = {RequestMethod.POST, RequestMethod.GET })
	public void export(@RequestParam(value = "params", required = false) String params,
			HttpServletResponse response) throws Exception {
		
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		JSONObject ob = new JSONObject();
		try {
			String result = aplservice.exportExcel(params);
			
			ob.put("path", result);
			ob.put("status", "success");
        	ob.put("message", "查询成功!");
			out.print(ob);
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
		
	}
	
	/**
	 * 根据ID递归查询子分类
	 * @param params
	 * @param response
	 * @throws Exception
	 * @author xucr
	 * @CreateDate 2020年6月23日
	 */
	@RequestMapping(value = "/queryClassificationNodeById", method = { RequestMethod.POST, RequestMethod.GET })
	public void queryClassificationNodeById(
			@RequestParam(value = "params", required = false) String params,
			HttpServletResponse response) throws Exception {
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		try {
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter out = response.getWriter();
//			Long id = (long) 0;
//			if(StringUtils.isNumeric(params)) {
//				id = Long.valueOf(params);
//			} 
			JSONArray nodes = new JSONArray();
			aplservice.recursionClassificationNodeById(null, params, true, nodes);
			
			JSONObject ob = new JSONObject();
			ob.put("status", "success");
        	ob.put("message", "查询成功!");
        	ob.put("data", nodes);
			out.print(ob.toString());
		} finally {
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	
	/**
	 * 查询某个分类的属性
	 * @author xucr
	 * @throws WTException 
	 * @throws WTRuntimeException 
	 * @throws IOException 
	 * @throws DocumentException 
	 * @throws JSONException 
	 * @CreateDate 2020年6月23日
	 */
	@RequestMapping(value = "/queryClassAttrs", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public void queryClassAttrs(
			@RequestParam(value = "params", required = false) String params,
			HttpServletResponse response) throws WTRuntimeException, WTException, DocumentException, IOException, JSONException {
		
		JSONObject ob = new JSONObject();
		JSONArray array = null;
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		if(StringUtils.isNumeric(params)) {
			Long nodeId = Long.valueOf(params);
			List<String> permission = APLConfig.getKeyToListByNode("wt.csm.navigation.ClassificationNode:" + nodeId);
			array = aplservice.getClassAttrsByClassId(nodeId, permission);
		}
		
//		if(null == array || array.length() == 0) {//在页面上操作
//			throw new WTException("您所选择的分类" + "未进行业务配置，请联系系统管理员");
//		}
		
		ob.put("status", "success");
    	ob.put("message", "查询成功!");
    	ob.put("data", array);
		out.print(ob.toString());
	}
	
	/**
	 *	获取合法值列表
	 * @param params
	 * @param response
	 * @author xucr
	 * @throws Exception 
	 * @CreateDate 2020年6月24日
	 */
	@RequestMapping(value = "/getLegalValue", method = { RequestMethod.POST, RequestMethod.GET })
	public void getLegalValue(
			@RequestParam(value = "params", required = false) String params,
			HttpServletResponse response) throws Exception {
		
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		JSONArray array = new JSONArray(params);
		JSONObject obj = array.getJSONObject(0);
		String typeName = obj.getString("typeName");
		String attrName = obj.getString("attrName");
		
		JSONObject ob = aplservice.getLegalValue(typeName, attrName);
		
		out.print(ob.toString());
	}
	
	/**
	 * 获取枚举值列表
	 * @param params
	 * @param response
	 * @throws Exception
	 * @author xucr
	 * @CreateDate 2020年6月28日
	 */
	@RequestMapping(value = "/getEnumeration", method = { RequestMethod.POST, RequestMethod.GET })
	public void getEnumeration(
			@RequestParam(value = "params", required = false) String params,
			HttpServletResponse response) throws Exception {
		
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		JSONArray array = new JSONArray(params);
		JSONObject obj = array.getJSONObject(0);
		String enumerName = obj.getString("enumerName");
		
		JSONObject ob = aplservice.getEnumeration(enumerName);
		
		out.print(ob.toString());
	}
	
	/**
	 * APL材料反查
	 * @param params
	 * @param response
	 * @throws Exception
	 * @author xucr
	 * @CreateDate 2020年7月28日
	 */
	@RequestMapping(value = "/reQueryAPL", method = {RequestMethod.POST, RequestMethod.GET })
	public void reQueryAPL(@RequestParam(value = "materialType", required = false) String materialType,
			HttpServletResponse response) throws Exception {
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
			String s = aplservice.reQueryAPL(materialType);
			out.print(s);
		}  catch (Exception e) {
			e.printStackTrace();
		} finally {
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
}